/**
 * 
 */
package za.co.sindi.oauth.core.context.web.portlet;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.portlet.PortletRequest;

import za.co.sindi.oauth.core.context.AttributeContext;
import za.co.sindi.oauth.core.context.ParameterContext;
import za.co.sindi.oauth.core.context.PropertyContext;
import za.co.sindi.oauth.core.context.RequestContext;

/**
 * @author Buhake Sindi
 * @since 25 March 2012
 *
 */
public class PortletRequestContext implements RequestContext, ParameterContext<String[]>, PropertyContext, AttributeContext {
	
	private PortletRequest portletRequest;

	/**
	 * 
	 * @param portletRequest
	 */
	public PortletRequestContext(PortletRequest portletRequest) {
		super();
		if (portletRequest == null) {
			throw new IllegalArgumentException("Portlet Request cannot be null.");
		}
		
		this.portletRequest = portletRequest;
	}

	/**
	 * @return the portletRequest
	 */
	public PortletRequest getPortletRequest() {
		return portletRequest;
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.AttributeContext#getAttribute(java.lang.String)
	 */
	@Override
	public Object getAttribute(String name) {
		// TODO Auto-generated method stub
		return getPortletRequest().getAttribute(name);
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.AttributeContext#getAttributeNames()
	 */
	@Override
	public String[] getAttributeNames() {
		// TODO Auto-generated method stub
		Enumeration<String> enums = getPortletRequest().getAttributeNames();
		if (enums == null) {
			return null;
		}
		
		List<String> names = new ArrayList<String>();
		while (enums.hasMoreElements()) {
			names.add(enums.nextElement());
		}
		
		return names.toArray(new String[names.size()]);
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.AttributeContext#removeAttribute(java.lang.String)
	 */
	@Override
	public void removeAttribute(String name) {
		// TODO Auto-generated method stub
		getPortletRequest().removeAttribute(name);
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.AttributeContext#setAttribute(java.lang.String, java.lang.Object)
	 */
	@Override
	public void setAttribute(String name, Object object) {
		// TODO Auto-generated method stub
		getPortletRequest().setAttribute(name, object);
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.PropertyContext#getProperties(java.lang.String)
	 */
	@Override
	public String[] getProperties(String name) {
		// TODO Auto-generated method stub
		Enumeration<String> enums = getPortletRequest().getProperties(name);
		if (enums == null) {
			return null;
		}
		
		List<String> properties = new ArrayList<String>();
		while (enums.hasMoreElements()) {
			properties.add(enums.nextElement());
		}
		
		return properties.toArray(new String[properties.size()]);
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.PropertyContext#getProperty(java.lang.String)
	 */
	@Override
	public String getProperty(String name) {
		// TODO Auto-generated method stub
		return getPortletRequest().getProperty(name);
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.PropertyContext#getPropertyNames()
	 */
	@Override
	public String[] getPropertyNames() {
		// TODO Auto-generated method stub
		Enumeration<String> enums = getPortletRequest().getPropertyNames();
		if (enums == null) {
			return null;
		}
		
		List<String> names = new ArrayList<String>();
		while (enums.hasMoreElements()) {
			names.add(enums.nextElement());
		}
		
		return names.toArray(new String[names.size()]);
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.ParameterContext#getParameter(java.lang.String)
	 */
	@Override
	public String getParameter(String name) {
		// TODO Auto-generated method stub
		return getPortletRequest().getParameter(name);
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.ParameterContext#getParameterNames()
	 */
	@Override
	public String[] getParameterNames() {
		// TODO Auto-generated method stub
		Enumeration<String> enums = getPortletRequest().getParameterNames();
		if (enums == null) {
			return null;
		}
		
		List<String> names = new ArrayList<String>();
		while (enums.hasMoreElements()) {
			names.add(enums.nextElement());
		}
		
		return names.toArray(new String[names.size()]);
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.ParameterContext#getParameterMap()
	 */
	@Override
	public Map<String, String[]> getParameterMap() {
		// TODO Auto-generated method stub
		return getPortletRequest().getParameterMap();
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.ParameterContext#getParameterValue(java.lang.String)
	 */
	@Override
	public String[] getParameterValue(String name) {
		// TODO Auto-generated method stub
		return getPortletRequest().getParameterValues(name);
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.RequestContext#getLocale()
	 */
	@Override
	public Locale getLocale() {
		// TODO Auto-generated method stub
		return getPortletRequest().getLocale();
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.RequestContext#getLocales()
	 */
	@Override
	public Locale[] getLocales() {
		// TODO Auto-generated method stub
		Enumeration<Locale> enums = getPortletRequest().getLocales();
		if (enums == null) {
			return null;
		}
		
		List<Locale> locales = new ArrayList<Locale>();
		while (enums.hasMoreElements()) {
			locales.add(enums.nextElement());
		}
		
		return locales.toArray(new Locale[locales.size()]);
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.RequestContext#getScheme()
	 */
	@Override
	public String getScheme() {
		// TODO Auto-generated method stub
		return getPortletRequest().getScheme();
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.RequestContext#isSecure()
	 */
	@Override
	public boolean isSecure() {
		// TODO Auto-generated method stub
		return getPortletRequest().isSecure();
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.RequestContext#getServerName()
	 */
	@Override
	public String getServerName() {
		// TODO Auto-generated method stub
		return getPortletRequest().getServerName();
	}

	/* (non-Javadoc)
	 * @see com.neurologic.oauth.core.context.RequestContext#getServerPort()
	 */
	@Override
	public int getServerPort() {
		// TODO Auto-generated method stub
		return getPortletRequest().getServerPort();
	}
}
